var o = {};

var news = new Function();
news.prototype = o;
/**
 * 选择
 */
o.checkAll = function(obj) {
	if (obj.attr('checked') == true) {
		$('.selectItem').attr('checked', 'checked');
	} else {
		$('.selectItem').attr('checked', '');
	}
}
o.SelectAllArea = function(obj) {
	if (obj.attr('checked') == true) {
		$('.ckbArea').attr('checked', 'checked');
	} else {
		$('.ckbArea').attr('checked', '');
	}
}
/**
* 删除
*/
o.delBatch = function() {
	var ids = "";
	$('.selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	$.ligerDialog.confirm('确定删除?', function(yes)
			{
				if(yes){
					$.ajax( {
						type : "POST",
						url : "news-delBatch.action",
						data : "ids="+ids,
						success : function(msg) {
							window.location.reload(false);
						},
						error:function (XMLHttpRequest, textStatus, errorThrown) {
						    alert("出错了："+errorThrown);
						}
					});
				}
			});
}
/**
 * 选择分类
 */
o.chooseCate=function(){
	$('<div id="choose_folder"></div>').appendTo($('body'));;
	$("#choose_folder").load(ROOT+'/category/category-show.action');
	$.ligerDialog.open({target:$("#choose_folder") , height: 300, width: 300,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var manager = $("#tree1").ligerGetTreeManager();
        		 	var note = manager.getSelected(); 
					$('#cateinput').text(note.data.text);
					$('#cateinputHidden').val(note.data.text);
					$('#cateIdHidden').val(note.data.id);
					dialog.close(); 
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
}
/**
 * 发表评论
 */
o.submitFaq=function(id,pid,type,categoryId){
	if($('#userId').val()=="" || !$('#userId').val()){
		alert('请先登录');
		return;
	}
	var oEditor = CKEDITOR.instances.faqContent;
	var content=oEditor.getData();
	if(content==""){
		alert("请输入评论内容!");
		return;
	}
	if(categoryId=='999'){
		categoryId='';
	}
	$.ajax( {
		type : "POST",
		url : ROOT+"/faq/faq-save.action",
		data : "from="+type+"&fkId="+id+"&parent="+pid+"&faq.content="+content+"&categoryId="+categoryId,
		success : function(msg) {
			//alert(msg.jsonResult);
			window.location.reload(false);
			//loadFaq();
			$('#faqContent').val("");
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    alert("出错了："+errorThrown);
		}
	});
}
 
o.feedback=function(faqId,content){
	if(content==""){
		alert("请输入评论内容!");
		return;
	}
	$.ajax( {
		type : "POST",
		url : ROOT+"/faq/faq-feedback.action",
		data : "faqId="+faqId+"&faq.content="+content,
		success : function(msg) {
			//alert(msg.jsonResult);
			loadFaq();
			$('#faqContent').val("");
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    alert("出错了："+errorThrown);
		}
	});
}

o.goPosition=function(id){
	if(!(/^\d+$/.test(id))){
		alert("请输入资讯的ID");
		return;
	}
	var ids = "";
	$('.selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要移动的记录!");
		return;
	}
	if($('.selectItem:checked').size()>1){
		alert("只能移动一条记录");
		return;
	}
	$.ajax( {
		type : "POST",
		url : ROOT+"/news/news-changePosition.action",
		data : "newsId="+id+"&ids="+ids,
		success : function(msg) {
			if(msg.jsonCode==0){
				alert(msg.jsonResult);
			}else{
				window.location.reload(false);
			}
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    alert("出错了："+errorThrown);
		}
	});
}

/**
* 添加我的收藏
*/
o.addFav=function(newsId){
	$.ajax( {
		type : "POST",
		url : ROOT+'/news/news-addFavorite.action', 
		data : 'newsId='+newsId, 
		success : function(msg) {
			$.ligerDialog.success(msg.jsonResult);
			$('#addFavorite').text('已收藏');
			$('#addFavorite').attr("href","");
			$('#addFavorite').css('color','gray');
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    $.ligerDialog.error("出错了："+errorThrown);

		}
	});
}

/**
* 删除我的收藏
*/
o.delFavorite = function() {
	var ids = "";
	$('#FAVORITE .selectItem').each( function() {
		if ($(this).attr("checked")) {
			if (ids == "")
				ids = $(this).val();
			else
				ids = ids + "," + $(this).val();
		}
	});
	if(ids==""){
		alert("请选择要删除的记录!");
		return;
	}
	$.ajax( {
		type : "POST",
		url : "index-delFavorite.action",
		data : "ids="+ids,
		success : function(msg) {
			//$.ligerDialog.success(msg.jsonResult);
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		    // 通常 textStatus 和 errorThrown 之中
		    $.ligerDialog.error("出错了："+errorThrown);
		}
	});
}

o.search=function(){
	var f=$('#searchForm');
	f.submit();
};
